<?
class ThumbImage{
	const IMAGEINTERLACE = 1, IMAGEDEBUG = 1,TextFont = 1;/* constants */
	public $ImageStream,$sFileLocation,$interlace,$type;/* properties */

	function __destruct(){unset($this->ImageStream);}
	
	private function printError($sMessage, $sMethod = __METHOD__, $sLine = __LINE__){
		if(ThumbImage::IMAGEDEBUG == 1){echo $sMethod . "(" . $sLine . ") " . $sMessage;}
	}

	private function loadImage(){/* load a image from file */
		if((int)ini_get("memory_limit")<64){@ini_set("memory_limit","64M");}
		switch($this->type){
			case 1:	$this->ImageStream = @imagecreatefromgif($this->sFileLocation);break;
			case 2:	$this->ImageStream = @imagecreatefromjpeg($this->sFileLocation);break;
			default:	$this->ImageStream = @imagecreatefrompng($this->sFileLocation);break;
		}
	}
	
	private function saveImage($quality = 100){
		if($quality<70 || $quality>100){
			$quality = 100;	
		}
		if (ThumbImage::IMAGEINTERLACE){
			imageinterlace($this->ImageStream, 1);
		}
		imagejpeg($this->ImageStream,$this->sFileLocation,$quality);
				
	}
	
	private function setLocations($sFileName,$get_direct=false){
		$this->sFileLocation =  $sFileName;
	}
	
	private function initializeImageProperties(){
		list($this->width, $this->height, $iType, $this->htmlattributes) = @getimagesize($this->sFileLocation);

		if (($this->width < 1) || ($this->height < 1)) {$this->printError('invalid imagesize',__METHOD__,__LINE__);}
		
		/* set the imahe type and mimetype */
		$this->type 	= $iType;
		$this->mimetype = 'image/jpeg';
	}

	public function loadfile($sFileName,$get_direct=false){
		$this->setLocations($sFileName,$get_direct);/* load an image from file into memory */
		$this->initializeImageProperties();
		$this->loadImage();
	}

	public function savefile($sFileName = NULL,$quality = 100){
		if ((isset($sFileName)) && ($sFileName != '')){$this->setLocations($sFileName);}
		$this->saveImage($quality);
	}

	public function resize($iNewWidth, $iNewHeight,$position = array()){
		
		/* resize the memoryimage do not keep ratio */
		if(function_exists("imagecopyresampled")){
			$ResizedImageStream = imagecreatetruecolor($iNewWidth, $iNewHeight);
			imagecopyresampled($ResizedImageStream, $this->ImageStream, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $this->width, $this->height);
		}
		else{
			$ResizedImageStream = imagecreate($iNewWidth, $iNewHeight);
			imagecopyresized($ResizedImageStream, $this->ImageStream, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $this->width, $this->height);
		}

		$this->ImageStream = $ResizedImageStream;
		$this->width = $iNewWidth;
		$this->height = $iNewHeight;
		//$this->setImageOrientation();
	}
	
	/* resize image to given width (keep ratio) */
	public function resizetowidth($iNewWidth){$iNewHeight = ($iNewWidth / $this->width) * $this->height;$this->resize($iNewWidth,$iNewHeight);}
	
	/* resize image to given height (keep ratio) */
	public function resizetoheight($iNewHeight){$iNewWidth = ($iNewHeight / $this->height) * $this->width;$this->resize($iNewWidth,$iNewHeight);}
	
	static function ThumbImageWithBackground($imageSource,$file_name,$width=0,$height=0,$position = array()){
		$quality = 100;
		if($imageSource == '' || (!$height && !$width)){
			echo 'not_file';
			exit();
		}
		
		$thumbDir="ad_images/";
		$get_direct_img = false;	
		
		$ThumbImage	=  &new ThumbImage();
		
		$ThumbImage->loadfile($imageSource,$get_direct_img);
		if(!$ThumbImage || !$ThumbImage->height || !$ThumbImage->width){
			return false;
		}
		
		self::create_thumb($ThumbImage,$width,$height,0,$position);
		$tempCacheFile = DIR_CACHE.'ad_images/'.$file_name;
		$ThumbImage->savefile($tempCacheFile,$quality);
	
	}
	static function create_thumb(&$ThumbImage,&$width,&$height,$limit_size = 0,$position = array()){
		//neu kich thuoc anh lon hon kich thuoc anh can crop
		if($ThumbImage->width >= $width || $ThumbImage->height >= $height){
			//check anh ty le anh bi co
			$width = ($width>0 && $ThumbImage->width <$width) ? $ThumbImage->width : $width;
			$height = ($height>0 && $ThumbImage->height <$height) ? $ThumbImage->height : $height;
			if(!empty($position) && isset($position['x1'])&& isset($position['x2'])&& isset($position['y1'])&& isset($position['y2'])){
				$position['x1'] = ($position['x1']>0) ? $position['x1'] : 0;
				$position['x2'] = ($position['x2']>0) ? $position['x2'] : 0;
				$position['y1'] = ($position['y1']>0) ? $position['y1'] : 0;
				$position['y2'] = ($position['y2']>0) ? $position['y2'] : 0;
				
				
				$w = ($position['x2'] > $position['x1']) ? ($position['x2'] - $position['x1']) : $ThumbImage->width;
				$h = ($position['y2'] > $position['y1']) ? ($position['y2'] - $position['y1']) : $ThumbImage->height;
				
				
				$image_bg= &new ThumbImage();
				$image_bg->loadfile(ROOT_PATH.'images/bg.jpg',true);
				$image_bg->resize($width,$height);
				$image_bg = $image_bg->ImageStream;
				imagecopyresampled($image_bg,$ThumbImage->ImageStream,0,0,$position['x1'],$position['y1'],$width,$height, $w, $h);
				imagedestroy($ThumbImage->ImageStream);
				$ThumbImage->ImageStream = $image_bg;
			}
			elseif($height > 0 || ($height > 0 && ($ThumbImage->width/$ThumbImage->height) < ($width/$height))){
				$ThumbImage->resizetoheight($height);
				$width_start=	($width - $ThumbImage->width)/2;
				
				$image_bg= &new ThumbImage();
				$image_bg->loadfile(ROOT_PATH.'images/bg.jpg',true);
				$image_bg->resize($width,$height);
				
				$image_bg = $image_bg->ImageStream;
				imagecopy($image_bg,$ThumbImage->ImageStream,$width_start,0,0,0, $ThumbImage->width, $height);
				imagedestroy($ThumbImage->ImageStream);
				$ThumbImage->ImageStream = $image_bg;
			}
			else{
				$ThumbImage->resizetowidth($width);
				$image_bg= &new ThumbImage();
				$image_bg->loadfile(ROOT_PATH.'images/bg.jpg',true);
				if(!$height){
					$height = ($width / $ThumbImage->width) * $ThumbImage->height;
				}
				if(!$width){
					$width = ($height / $ThumbImage->height) * $ThumbImage->width;
				}
				$image_bg->resize($width,$height);
				$image_bg = $image_bg->ImageStream;
				
				imagecopy($image_bg,$ThumbImage->ImageStream,0,0,0,0, $width, $ThumbImage->height);
				imagedestroy($ThumbImage->ImageStream);
				$ThumbImage->ImageStream = $image_bg;
			}
		}
		else{
			$width_start	=	($width - $ThumbImage->width)/2;
			$height_start	=	($height-$ThumbImage->height)/2;
			
			$image_bg= &new ThumbImage();
			$image_bg->loadfile(ROOT_PATH.'images/bg.jpg',true);
			if(!$height){
				$height = ($width / $ThumbImage->width) * $ThumbImage->height;
			}
			if(!$width){
				$width = ($height / $ThumbImage->height) * $ThumbImage->width;
			}
			$image_bg->resize($width,$height);
			
			$image_bg	=	$image_bg->ImageStream;
			imagecopy($image_bg,$ThumbImage->ImageStream,$width_start,$height_start,0,0, $ThumbImage->width, $ThumbImage->height);
			imagedestroy($ThumbImage->ImageStream);
			$ThumbImage->ImageStream = $image_bg;
		}
	}
}
?>